package com.qsbc.pygram.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsbc.pygram.common.Result;
import com.qsbc.pygram.entity.ProgramQuestion;
import com.qsbc.pygram.service.ProgramQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-02-01
 */
@RestController
@RequestMapping("/pygram/program-question")
public class ProgramQuestionController {

    @Autowired
    private ProgramQuestionService programQuestionService;

    @PostMapping("/programQuestion")
    public Result add(@RequestBody ProgramQuestion programQuestion) {
        int res = programQuestionService.add(programQuestion);
        if (res != 0) {
            return Result.success().success(res);
        }
        return Result.error().error();
    }

    @GetMapping("{questionId}")
    public Result findOnlyQuestionId(
            @PathVariable Integer questionId){
        ProgramQuestion programQuestion = programQuestionService.getById(questionId);
        return Result.success().success(programQuestion);
    }
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String subject,
                           @RequestParam(defaultValue = "") String section,
                           @RequestParam(defaultValue = "") String level) {

        return Result.success(programQuestionService.findPage(new Page<>(pageNum, pageSize), subject,section,level));
    }

    @PostMapping
    public Result save(@RequestBody ProgramQuestion programQuestion) {
        return Result.success(programQuestionService.saveOrUpdate(programQuestion));
    }
    @DeleteMapping("{questionId}")
    public Result delete(@PathVariable Integer questionId) {
        return Result.success(programQuestionService.removeById(questionId));
    }


    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> questionIds) {
        return Result.success(programQuestionService.removeByIds(questionIds));
    }

}
